/*
成就
*/
window._MCJ = {

    _MAINLABEL:null,

    _CJLIST:[
        {
        type:'npc',name:'怪物图鉴收集',
        rewards:[
                    {per:0.1,reward:{type:'银葫芦',value:100}},
                    {per:0.2,reward:{type:'银葫芦',value:100}},
                    {per:0.3,reward:{type:'金葫芦',value:100}},
                    {per:0.4,reward:{type:'金葫芦',value:100}},
                    {per:0.5,reward:{type:'老葫芦',value:100}},
                    {per:0.6,reward:{type:'老葫芦',value:100}},
                    {per:0.8,reward:{type:'重生丹',value:1}},
                    {per:0.9,reward:{type:'重生丹',value:1}},
                ]
        },
        {
            type:'xiuwei',name:'修为收集',
            rewards:[
                        {per:0.1,reward:{type:'银葫芦',value:100}},
                        {per:0.2,reward:{type:'银葫芦',value:100}},
                        {per:0.3,reward:{type:'金葫芦',value:100}},
                        {per:0.4,reward:{type:'金葫芦',value:100}},
                        {per:0.5,reward:{type:'老葫芦',value:100}},
                        {per:0.6,reward:{type:'老葫芦',value:100}},
                        {per:0.8,reward:{type:'大师葫芦',value:3}},
                        {per:0.9,reward:{type:'大师葫芦',value:5}},
                    ],
        },
        {
            type:'skill',name:'技能收集',
            rewards:[
                        {per:0.1,reward:{type:'银葫芦',value:100}},
                        {per:0.2,reward:{type:'银葫芦',value:100}},
                        {per:0.3,reward:{type:'金葫芦',value:100}},
                        {per:0.4,reward:{type:'金葫芦',value:100}},
                        {per:0.5,reward:{type:'老葫芦',value:100}},
                        {per:0.6,reward:{type:'老葫芦',value:100}},
                        {per:0.8,reward:{type:'大师葫芦',value:3}},
                        {per:0.9,reward:{type:'大师葫芦',value:5}},
                    ],
        },
        {
            type:'skillbd',name:'被动技能收集',
            rewards:[
                        {per:0.1,reward:{type:'银葫芦',value:100}},
                        {per:0.2,reward:{type:'银葫芦',value:100}},
                        {per:0.3,reward:{type:'金葫芦',value:100}},
                        {per:0.4,reward:{type:'金葫芦',value:100}},
                        {per:0.5,reward:{type:'老葫芦',value:100}},
                        {per:0.6,reward:{type:'老葫芦',value:100}},
                        {per:0.8,reward:{type:'大师葫芦',value:3}},
                        {per:0.9,reward:{type:'大师葫芦',value:5}},
                    ],
        },
        {
            type:'equip',name:'装备收集',
            rewards:[
                        {per:0.1,reward:{type:'银葫芦',value:100}},
                        {per:0.2,reward:{type:'银葫芦',value:100}},
                        {per:0.3,reward:{type:'金葫芦',value:100}},
                        {per:0.4,reward:{type:'金葫芦',value:100}},
                        {per:0.5,reward:{type:'老葫芦',value:100}},
                        {per:0.6,reward:{type:'老葫芦',value:100}},
                        {per:0.7,reward:{type:'大师葫芦',value:2}},
                        {per:0.8,reward:{type:'大师葫芦',value:3}},
                    ],
        },
        {
            type:'fsx',name:'副属性收集',
            rewards:[
                        {per:0.1,reward:{type:'银葫芦',value:100}},
                        {per:0.2,reward:{type:'银葫芦',value:100}},
                        {per:0.3,reward:{type:'金葫芦',value:100}},
                        {per:0.4,reward:{type:'金葫芦',value:100}},
                        {per:0.5,reward:{type:'老葫芦',value:100}},
                        {per:0.6,reward:{type:'老葫芦',value:100}},
                        {per:0.8,reward:{type:'大师葫芦',value:3}},
                        {per:0.9,reward:{type:'重生丹',value:1}},
                    ],
        },
        {
            type:'status',name:'状态收集',
            rewards:[
                        {per:0.1,reward:{type:'银葫芦',value:100}},
                        {per:0.2,reward:{type:'银葫芦',value:100}},
                        {per:0.3,reward:{type:'金葫芦',value:100}},
                        {per:0.4,reward:{type:'金葫芦',value:100}},
                        {per:0.5,reward:{type:'老葫芦',value:100}},
                        {per:0.6,reward:{type:'老葫芦',value:100}},
                        {per:0.7,reward:{type:'大师葫芦',value:2}},
                        {per:0.8,reward:{type:'大师葫芦',value:3}},
                    ],
        },
        {
            type:'item',name:'道具收集',
            rewards:[
                        {per:0.1,reward:{type:'银葫芦',value:100}},
                        {per:0.2,reward:{type:'银葫芦',value:100}},
                        {per:0.3,reward:{type:'金葫芦',value:100}},
                        {per:0.4,reward:{type:'金葫芦',value:100}},
                        {per:0.5,reward:{type:'老葫芦',value:100}},
                        {per:0.6,reward:{type:'老葫芦',value:100}},
                        {per:0.7,reward:{type:'大师葫芦',value:2}},
                        {per:0.8,reward:{type:'大师葫芦',value:3}},
                    ],
        }
    ],

    showWork:function(parentNode,param){

        var uiObj = _GRM.getNodeObj('cenjiu');
        var mainLabel = uiObj.node;
        _MCJ._MAINLABEL = mainLabel;
        if(uiObj.isnew){
            _MCJ.initButton(mainLabel);
        }
        _MCJ.refresh();
        parentNode.addChild(mainLabel);
        return mainLabel;

    },


    refresh:function(){
        var nodeList = _MCJ._MAINLABEL.getChildByName('leftLabel')
                                    .getChildByName('troops')
                                    .getChildByName('troops')
                                    .getChildren();
        for(var i=0;i<_MCJ._CJLIST.length;i++){
            _MCJ.refreshOne(nodeList[i],_MCJ._CJLIST[i]);
            nodeList[i].cjIndex = i;
        }
    },

    refreshOne:function(node,cjObj){
        var titleNode = node.getChildByName('title');
        var expNode = node.getChildByName('exp');
        var btnReward = node.getChildByName('btnReward');

        titleNode.getComponent(cc.Label).string = cjObj.name;
        expNode.getChildByName("progress").getComponent(cc.ProgressBar).progress = _MCJ.getDataOfCj(cjObj).per;
        expNode.getChildByName('title').getComponent(cc.Label).string = _MCJ.getWorkInfoContent(cjObj);
        if(_MCJ.isComplete(cjObj)){
            btnReward.active = true;
        }else{
            btnReward.active = false;
        }
    },


    initButton:function(mainLabel){
        var xianqinlabel = mainLabel.getChildByName('xianqin');
        var xianqinmasklabel = mainLabel.getChildByName('xianqin').getChildByName('mask');
        xianqinmasklabel.on(cc.Node.EventType.TOUCH_START, function(){
            this.active = false;
        }, xianqinlabel);
     
        mainLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, mainLabel);

        var nodeList = _MCJ._MAINLABEL.getChildByName('leftLabel')
                                    .getChildByName('troops')
                                    .getChildByName('troops')
                                    .getChildren();
        
        for(var i=0;i<nodeList.length;i++){
            var node = nodeList[i];
            var btninfo = node.getChildByName('buttonInfo');
            var btnReward = node.getChildByName('btnReward');
            btninfo.on(cc.Node.EventType.TOUCH_START, function(){
                _MCJ.showDetail(this.cjIndex);
            }, node);
            btnReward.on(cc.Node.EventType.TOUCH_START, function(){
                //console.log('奖励',this.cjIndex);
                _MCJ.completeWork(this.cjIndex);
            }, node);
        }
    },

    showDetail:function(cjIndex){
        var xianqinlabel = _MCJ._MAINLABEL.getChildByName('xianqin');
        var xiangqinNodes = xianqinlabel.getChildByName('bg_wupin').getChildren();
        //console.log('详情',cjIndex);
        var config = _MCJ._CJLIST[cjIndex];
        var type = config.type;
        for(var i=0;i<config.rewards.length;i++){
            var cjkey = type+"_"+i;
            var per = (config.rewards[i].per*100).toFixed(0)+"%"
            var msg = "进度:"+per; 
            msg+=" 奖励"+config.rewards[i].reward.type+":"+config.rewards[i].reward.value;
            if(_GAMEDATAHELPER.hasGetCj(cjkey)){
                msg+=" 已领取";
            }
            //console.log(msg);
            if(xiangqinNodes[i]){
                xiangqinNodes[i].getChildByName('nmb').getComponent(cc.Label).string = msg;
            }
        }
        xianqinlabel.active = true;
    },

    completeWork:function(workIndex){
        var config = _MCJ._CJLIST[workIndex];
        var type = config.type;
        var data = _MCJ.getDataOfCj(config);
        var per = data.per;

        for(var i=0;i<config.rewards.length;i++){
            var cjkey = type+"_"+i;
            if(per>=config.rewards[i].per
                &&!_GAMEDATAHELPER.hasGetCj(cjkey)
            ){
                var reward = config.rewards[i].reward;
                if(reward.type=="gold"){
                    _GAMEDATAHELPER.oprMoney(reward.value);
                    _TIPS.showMessage("获取金钱:"+reward.value);
                }else{
                    var itemObj = _IOF.createObjByName(reward.type,null);
                    itemObj.nmb = reward.value;
                    _GAMEDATAHELPER.pushItem(itemObj);
                    _TIPS.showMessage("获取"+reward.type+":"+reward.value);
                }
                _GAMEDATAHELPER.signCjLog(cjkey);
                _MCJ.refresh();
                break;
            }
        }
    },

    isComplete:function(config){
        var type = config.type;
        var data = _MCJ.getDataOfCj(config);
        var per = data.per;
        for(var i=0;i<config.rewards.length;i++){
            var cjkey = type+"_"+i;
            if(per>=config.rewards[i].per
                &&!_GAMEDATAHELPER.hasGetCj(cjkey)
            ){
                return true;
            }
        }

        return false;
    },
    
    getWorkInfoContent:function(config){
        var data = _MCJ.getDataOfCj(config);
        if(data.total!=0){
            return "进度:"+(data.per*100).toFixed(1)+"%("+data.current+"/"+data.total+")";
        }else{
            return "未知";
        }
    },
    

    getDataOfCj:function(config){
        var type = config.type;
        var total = 0;
        var current = 0;
        if(type=="npc"){
            for(var key in _NPC){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(key)){
                    current++;
                }
            }
        }else if(type=="xiuwei"){
            for(var key in _XW){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(_XW[key].label)){
                    current++;
                }
            }
        }else if(type=="skill"){
            for(var key in _SKILL){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(key)){
                    current++;
                }
            }
        }else if(type=="skillbd"){
            for(var key in _SKILLBD){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(key)){
                    current++;
                }
            }
        }else if(type=="equip"){
            for(var key in _EQUIP){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(key)){
                    current++;
                }
            }
        }else if(type=="item"){
            for(var key in _FIGHTITEM){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(key)){
                    current++;
                }
            }
        }else if(type=="fsx"){
            for(var key in _DICT){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(_DICT[key])){
                    current++;
                }
            }
        }else if(type=="status"){
            for(var key in _STATUSCONFCHESS){
                total++;
                if(_GAMEDATAHELPER.hasNpcTj(_STATUSCONFCHESS[key].name)){
                    current++;
                }
            }
        }

        return {total:total,current:current,per:(total?current/total:0)};
    },

    testTypeAndNmb:function(nmb){
        var total = 0;
        for(var key in _NPC){
            total++;
            _GAMEDATAHELPER.signNpcTj(key);
            if(total>=nmb)
                break;
        }

    }

}